package com.xbts.common.core.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import com.xbts.common.core.enums.DateTypeEnum;
import org.apache.commons.lang3.time.DateFormatUtils;

import javax.validation.constraints.NotNull;

/**
 * 时间工具类
 *
 * @author xbts
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";
    public static String MM = "MM";
    public static String DD = "dd";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String MM_DD = "MM/dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算时间差
     *
     * @param endDate   最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static String timeDistance(Date endDate, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 获取今天的开始结束时间
     * @return 今天的开始结束时间
     */
    public static List<String> getTodayDate() {
        LocalDate today = LocalDate.now(); // 获取今天日期
        return getDatetimeByLocalDate(today);
    }

    /**
     * 获取昨天的日期
     * @return 昨天的日期
     */
    public static List<String> getYesterdayDate() {
        LocalDate today = LocalDate.now().minusDays(1);
        return getDatetimeByLocalDate(today);
    }

    /**
     * 获取指定日期的开始结束时间
     * @param localDate 日期
     * @return 指定日期的开始结束时间
     */
    @NotNull
    private static List<String> getDatetimeByLocalDate(LocalDate localDate) {
        List<String> result = new ArrayList<>(2);
        LocalDateTime startDateTime = LocalDateTime.of(localDate, LocalTime.MIN); // 今天的开始时间是当天的0点0分0秒
        LocalDateTime endDateTime = LocalDateTime.of(localDate, LocalTime.MAX); // 今天的结束时间是当天的23点59分59秒999纳秒
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS); // 定义日期时间格式
        result.add(startDateTime.format(formatter)); // 开始时间
        result.add(endDateTime.format(formatter)); // 结束时间
        return result;
    }

    /**
     * 获取本月每天的开始时间和结束时间
     * @return
     */
    public static List<List<String>> getAllDaysOfThisMonth(){
        // 获取当前日期
        LocalDate now = LocalDate.now();

        // 获取本月第一天的日期
        LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());

        // 获取下个月第一天的日期
        LocalDate firstDayOfNextMonth = now.plusMonths(1).with(TemporalAdjusters.firstDayOfMonth());

        // 循环遍历本月的每一天，并输出开始时间和结束时间
        LocalDate date = firstDayOfMonth;
        List<List<String>> result = new ArrayList<>();
        while (date.isBefore(firstDayOfNextMonth)) {
            List<String> dayList = new ArrayList<>(2);
            dayList.add(date + " 00:00:00");
            dayList.add(date + " 23:59:59");
            result.add(dayList);
            date = date.plusDays(1);
        }
        return result;
    }
    /**
     * 获取最近7天 每一天的 开始时间和结束时间
     * @return
     */
    public static List<List<String>> Last7DaysRange(){
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 格式化日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

        List<List<String>> result = new ArrayList<>();
        for (int i = 6; i >= 0; i--) {
            LocalDate date = today.minusDays(i);
            LocalDateTime startOfDay = LocalDateTime.of(date, LocalDateTime.MIN.toLocalTime());
            LocalDateTime endOfDay = LocalDateTime.of(date, LocalDateTime.MAX.toLocalTime());
            List<String> dayList = new ArrayList<>(2);
            dayList.add(startOfDay.format(formatter));
            dayList.add(endOfDay.format(formatter));
            result.add(dayList);
        }
        return result;
    }
    /**
     * 获取最近10天 每一天的 开始时间和结束时间
     * @return
     */
    public static List<List<String>> Last10DaysRange(){
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 格式化日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<List<String>> result = new ArrayList<>();
        for (int i = 9; i >= 0; i--) {
            LocalDate date = today.minusDays(i);
            LocalDateTime startOfDay = LocalDateTime.of(date, LocalDateTime.MIN.toLocalTime());
            LocalDateTime endOfDay = LocalDateTime.of(date, LocalDateTime.MAX.toLocalTime());
            List<String> dayList = new ArrayList<>(2);
            dayList.add(startOfDay.format(formatter));
            dayList.add(endOfDay.format(formatter));
            result.add(dayList);
        }
        return result;
    }
    /**
     * 获取本年每月的开始时间和结束时间
     * @return
     */
    public static List<List<String>> getMonthsOfThisYear(){
        // 获取当前年份
        int currentYear = YearMonth.now().getYear();

        List<List<String>> result = new ArrayList<>();
        // 循环遍历当年的每个月，并输出开始时间和结束时间
        for (int month = 1; month <= 12; month++) {
            YearMonth yearMonth = YearMonth.of(currentYear, month);
            LocalDate firstDayOfMonth = yearMonth.atDay(1);
            LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
            List<String> monthList = new ArrayList<>(2);
            monthList.add(firstDayOfMonth + " 00:00:00");
            monthList.add(lastDayOfMonth + " 23:59:59");
            result.add(monthList);
        }
        return result;
    }
    public static List<List<String>> getLast12MonthsRange () {
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 计算当前月份的第一天
        LocalDate currentMonthStart = today.withDayOfMonth(1);

        // 初始化日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<List<String>> result = new ArrayList<>();
        // 遍历最近12个月，从最远的月份到最近的月份
        for (int i = 11; i >= 0; i--) {
            // 计算当前月份的开始时间和结束时间
            LocalDate startOfMonth = currentMonthStart.minusMonths(i);
            LocalDate endOfMonth = startOfMonth.plusMonths(1).minusDays(1);

            // 将 LocalDate 转换为 LocalDateTime
            LocalDateTime startOfMonthTime = startOfMonth.atStartOfDay();
            LocalDateTime endOfMonthTime = endOfMonth.atTime(LocalTime.MAX);

            List<String> monthList = new ArrayList<>(2);
            monthList.add(startOfMonthTime.format(formatter));
            monthList.add(endOfMonthTime.format(formatter));
            result.add(monthList);

        }
        return result;
    }

    /**
     * 根据时间类型获取具体每一天或每一周或每一月的开始时间和结束时间
     * @return
     */
    public static List<List<String>> getDateListByDateTypeEnum(DateTypeEnum dateTypeEnum){

        if(dateTypeEnum == null){
            return null;
        }
        switch (dateTypeEnum){
            case DAY:
                return getAllDaysOfThisMonth();
            case WEEK:
                return getTheEightFourWeeksEveryDay();
            case MONTH:
                return getLast12MonthsRange();
            default:
                return null;
        }
    }

    /**
     * 根据时间类型获取当天或当周或当月开始时间和结束时间
     * @return
     */
    public static List<String> getDateByDateTypeEnum(DateTypeEnum dateTypeEnum){
        List<String> result = new ArrayList<>(2);
        // 获取当前日期
        LocalDate today = LocalDate.now();

        switch (dateTypeEnum){
            case DAY:
                result.add(today + " 00:00:00");
                result.add(today + " 23:59:59");
                return result;
            case WEEK:
                // 获取本周的开始时间（周一）
                LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                // 获取本周的结束时间（周日）
                LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                result.add(startOfWeek + " 00:00:00");
                result.add(endOfWeek + " 23:59:59");
                return result;
            case MONTH:
                // 获取本月的开始时间（月初）
                LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
                // 获取本月的结束时间（月末）
                LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
                result.add(startOfMonth + " 00:00:00");
                result.add(endOfMonth + " 23:59:59");
                return result;
            default:
                return null;
        }
    }


    /**
     * 获取当天的日期范围
     * @return 本周的日期
     */
    public static List<String> getTodayRange() {
        // 获取昨天的日期
        LocalDate yesterday = LocalDate.now();

        // 定义开始时间和结束时间
        LocalDateTime startOfYesterday = yesterday.atStartOfDay();
        LocalDateTime endOfYesterday = yesterday.atTime(LocalTime.MAX);

        // 输出开始时间和结束时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfYesterday.format(formatter));
        result.add(endOfYesterday.format(formatter));
        return result;
    }
    /**
     * 获取昨天的日期范围
     * @return 本周的日期
     */
    public static List<String> getYesterdayRange() {
        // 获取昨天的日期
        LocalDate yesterday = LocalDate.now().minusDays(1);

        // 定义开始时间和结束时间
        LocalDateTime startOfYesterday = yesterday.atStartOfDay();
        LocalDateTime endOfYesterday = yesterday.atTime(LocalTime.MAX);

        // 输出开始时间和结束时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfYesterday.format(formatter));
        result.add(endOfYesterday.format(formatter));
        return result;
    }
    /**
     * 获取最近7天的日期范围
     * @return 本周的日期
     */
    public static List<String> getLast7DaysRange() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 计算过去7天的第一个和最后一个日期
        LocalDate firstDay = today.minusDays(6);
        LocalDate lastDay = today;

        // 确定开始时间和结束时间
        LocalDateTime startOfFirstDay = firstDay.atStartOfDay();
        LocalDateTime endOfLastDay = lastDay.atTime(LocalTime.MAX);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfFirstDay.format(formatter));
        result.add(endOfLastDay.format(formatter));
        return result;
    }
    /**
     * 获取最近100天的日期范围
     * @return 本周的日期
     */
    public static List<String> getLast100DaysRange() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 计算过去7天的第一个和最后一个日期
        LocalDate firstDay = today.minusDays(99);
        LocalDate lastDay = today;

        // 确定开始时间和结束时间
        LocalDateTime startOfFirstDay = firstDay.atStartOfDay();
        LocalDateTime endOfLastDay = lastDay.atTime(LocalTime.MAX);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfFirstDay.format(formatter));
        result.add(endOfLastDay.format(formatter));
        return result;
    }
    /**
     * 获取最近30天的日期范围
     * @return 本周的日期
     */
    public static List<String> getLast30DaysRange() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 计算过去7天的第一个和最后一个日期
        LocalDate firstDay = today.minusDays(29);
        LocalDate lastDay = today;

        // 确定开始时间和结束时间
        LocalDateTime startOfFirstDay = firstDay.atStartOfDay();
        LocalDateTime endOfLastDay = lastDay.atTime(LocalTime.MAX);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfFirstDay.format(formatter));
        result.add(endOfLastDay.format(formatter));
        return result;
    }
    /**
     * 获取最近21天的日期范围
     * @return 本周的日期
     */
    public static List<String> getLast21DaysRange() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 计算过去7天的第一个和最后一个日期
        LocalDate firstDay = today.minusDays(20);
        LocalDate lastDay = today;

        // 确定开始时间和结束时间
        LocalDateTime startOfFirstDay = firstDay.atStartOfDay();
        LocalDateTime endOfLastDay = lastDay.atTime(LocalTime.MAX);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfFirstDay.format(formatter));
        result.add(endOfLastDay.format(formatter));
        return result;
    }
    /**
     * 获取最近21天的日期范围
     * @return 本周的日期
     */
    public static List<List<String>> getLast21DaysRangeList() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        List<List<String>> result = new ArrayList<>();
        for (int i = 20; i >= 0; i--) {
            LocalDate firstDay = today.minusDays(i);

            // 确定开始时间和结束时间
            LocalDateTime startOfFirstDay = firstDay.atStartOfDay();
            LocalDateTime endOfLastDay = firstDay.atTime(LocalTime.MAX);

            // 定义日期格式化器
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            List<String> result1 = new ArrayList<>(2);
            result1.add(startOfFirstDay.format(formatter));
            result1.add(endOfLastDay.format(formatter));
            result.add(result1);
        }
        return result;
    }

    /**
     * 获取本周的日期
     * @return 本周的日期
     */
    public static List<String> getCurrentWeek() {
        LocalDate today = LocalDate.now();
        // 获取本周的开始时间和结束时间
        LocalDate thisWeekStart = today.with(DayOfWeek.MONDAY);
        LocalDate thisWeekEnd = today.with(DayOfWeek.SUNDAY);

        List<String> result = new ArrayList<>(2);
        result.add(thisWeekStart + " 00:00:00");
        result.add(thisWeekEnd + " 23:59:59");
        return result;
    }

    /**
     * 获取上周的日期
     * @return 上周的日期
     */
    public static List<String> getLastWeek() {
        LocalDate today = LocalDate.now();
        // 获取上周的开始时间和结束时间
        LocalDate lastWeekStart = today.minusWeeks(1).with(DayOfWeek.MONDAY);
        LocalDate lastWeekEnd = today.minusWeeks(1).with(DayOfWeek.SUNDAY);

        List<String> result = new ArrayList<>(2);
        result.add(lastWeekStart + " 00:00:00");
        result.add(lastWeekEnd + " 23:59:59");
        return result;
    }

    /**
     * 获取前四周每一周开始结束日期
     * @return 前四周每一周开始结束日期
     */
    public static List<List<String>> getTheEightFourWeeksEveryDay(){
        // 获取当前日期
        LocalDate now = LocalDate.now();

        // 设置为本周的第一天（默认以周日为一周的第一天）
        LocalDate start = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

        List<List<String>> result = new ArrayList<>(8);
        // 循环获取已结束的四周每周的开始时间和结束时间
        for (int i = 0; i < 8; i++) {
            LocalDate end = start.plusDays(6);
            List<String> weekList = new ArrayList<>(2);
            weekList.add(start + " 00:00:00");
            weekList.add(end + " 23:59:59");
            result.add(weekList);
            start = start.minusWeeks(1);
        }
        Collections.reverse(result);
        return result;
    }

    /**
     * 获取前四周开始结束日期
     * @return 前四周开始结束日期
     */
    public static List<String> getTheLastFourWeeks(){
        List<String> result = new ArrayList<>(2);
        LocalDate today = LocalDate.now();
        // 计算前四周的开始时间和结束时间
        LocalDate fourWeeksAgoStart = today.minusWeeks(4).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate fourWeeksAgoEnd = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
        result.add(fourWeeksAgoStart + " 00:00:00");
        result.add(fourWeeksAgoEnd + " 23:59:59");
        return result;
    }
    /**
     * 获取最近6个月的月份 yy/MM 格式
     */
    public static List<String> getLastSixMonths(){
        List<String> months = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy/MM");

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取最近6个月的日期
        for (int i = 5; i >= 0; i--) {
            LocalDate date = currentDate.minusMonths(i);
            months.add(date.format(formatter));
        }
        return months;
    }
    /**
     * 获取最近12个月的月份 yy/MM 格式
     */
    public static List<String> getLastTwelveMonths(){
        List<String> months = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy/MM");

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取最近12个月的日期
        for (int i = 11; i >= 0; i--) {
            LocalDate date = currentDate.minusMonths(i);
            months.add(date.format(formatter));
        }
        return months;
    }

    /**
     * 获取最近10天 MM/dd 格式
     */
    public static List<String> getLastTenDays() {
        List<String> days = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取最近10天的日期
        for (int i = 9; i >= 0; i--) {
            LocalDate date = currentDate.minusDays(i);
            days.add(date.format(formatter));
        }
        return days;
    }
    /**
     * 获取最近21天 MM/dd 格式
     */
    public static List<String> getLast21Days() {
        List<String> days = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取最近10天的日期
        for (int i = 20; i >= 0; i--) {
            LocalDate date = currentDate.minusDays(i);
            days.add(date.format(formatter));
        }
        return days;
    }
    /**
     * 获取最近10天开始和结束时间
     */
    public static List<String> getLastTenDayRange() {
        List<String> days = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate today = LocalDate.now();
        // 第一日的开始时间（最近 10 天的第一天）
        LocalDate firstDay = today.minusDays(9);
        LocalDateTime startOfFirstDay = firstDay.atStartOfDay();
        // 最后一日的结束时间（今天）
        LocalDateTime endOfLastDay = today.atTime(LocalTime.MAX);

        days.add(startOfFirstDay.format(formatter));
        days.add(endOfLastDay.format(formatter));
        return days;
    }
    /**
     * 获取最近7天 MM/dd 格式
     */
    public static List<String> getLastSevenDays() {
        List<String> days = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取最近10天的日期
        for (int i = 6; i >= 0; i--) {
            LocalDate date = currentDate.minusDays(i);
            days.add(date.format(formatter));
        }
        return days;
    }
    /**
     * 获取最近3个月 格式： MM月
     */
    public static List<String> getPastThreeMonths() {
        // 创建一个日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM月");

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 存储最近3个月的日期
        List<String> recentMonths = new ArrayList<>();

        // 遍历最近3个月
        for (int i = 2; i >= 0; i--) {
            // 获取当前月的开始日期
            LocalDate firstDayOfMonth = today.minusMonths(i).withDayOfMonth(1);
            // 格式化为 MM月
            String formattedMonth = firstDayOfMonth.format(formatter);
            recentMonths.add(formattedMonth);
        }
        return recentMonths;
    }

    public static List<String> getLastFourWeeks() {
        List<String> weeks = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 计算本周的开始日期（周一）
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
        if (startOfWeek.isAfter(today)) {
            startOfWeek = startOfWeek.minusWeeks(1);
        }

        // 获取最近4周的日期范围
        for (int i = 0; i < 4; i++) {
            LocalDate endOfWeek = startOfWeek.plusDays(6); // 一周的结束日期是开始日期加6天
            if (i == 0) {
                // 最近一周结束日期设置为今天
                endOfWeek = today;
            }
            weeks.add(startOfWeek.format(formatter) + "-" + endOfWeek.format(formatter));

            // 更新到上一周的开始日期
            startOfWeek = startOfWeek.minusWeeks(1);
        }

        // 由于我们从最近一周开始，因此需要将列表反转以从旧到新排序
        List<String> reversedWeeks = new ArrayList<>();
        for (int i = weeks.size() - 1; i >= 0; i--) {
            reversedWeeks.add(weeks.get(i));
        }

        return reversedWeeks;
    }
    public static List<String> getLastEightWeeks() {
        List<String> weeks = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 计算本周的开始日期（周一）
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
        if (startOfWeek.isAfter(today)) {
            startOfWeek = startOfWeek.minusWeeks(1);
        }

        // 获取最近8周的日期范围
        for (int i = 0; i < 8; i++) {
            LocalDate endOfWeek = startOfWeek.plusDays(6); // 一周的结束日期是开始日期加6天
            if (i == 0) {
                // 最近一周结束日期设置为今天
                endOfWeek = today;
            }
            weeks.add(startOfWeek.format(formatter) + "-" + endOfWeek.format(formatter));

            // 更新到上一周的开始日期
            startOfWeek = startOfWeek.minusWeeks(1);
        }

        // 由于我们从最近一周开始，因此需要将列表反转以从旧到新排序
        List<String> reversedWeeks = new ArrayList<>();
        for (int i = weeks.size() - 1; i >= 0; i--) {
            reversedWeeks.add(weeks.get(i));
        }

        return reversedWeeks;
    }
    public static List<String> getDaysOfMonth() {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 获取当前月份
        YearMonth yearMonth = YearMonth.from(today);

        // 获取当前月份的天数
        int daysInMonth = yearMonth.lengthOfMonth();

        // 创建一个存储天数的集合
        List<String> days = new ArrayList<>();

        // 将每一天的日期添加到集合中
        for (int day = 1; day <= daysInMonth; day++) {
            days.add(day + "");
        }
        return days;
    }
    public static Integer lengthOfMonth () {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 获取当前月份
        YearMonth yearMonth = YearMonth.from(today);

        // 获取当前月份的天数
        return yearMonth.lengthOfMonth();
    }
    /**
     * 获取本月的开始、结束日期
     * @return 本月的开始、结束日期
     */
    public static List<String> getCurrentMonth() {
        LocalDate today = LocalDate.now();
        // 获取本月的开始时间和结束时间
        LocalDate thisMonthStart = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate thisMonthEnd = today.with(TemporalAdjusters.lastDayOfMonth());
        List<String> result = new ArrayList<>(2);
        result.add(thisMonthStart.toString() + " 00:00:00");
        result.add(thisMonthEnd.toString() + " 23:59:59");
        return result;
    }
    /**
     * 获取去年本月的开始、结束日期
     * @return 本月的开始、结束日期
     */
    public static List<String> getLastYearMonthRange() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取当前月份和年份
        int currentMonth = currentDate.getMonthValue();
        int currentYear = currentDate.getYear();

        // 计算去年的年份
        int lastYear = currentYear - 1;

        // 去年本月的第一天
        LocalDateTime startOfMonthLastYear = LocalDateTime.of(lastYear, currentMonth, 1, 0, 0, 0);

        // 去年本月的最后一天
        LocalDateTime endOfMonthLastYear = LocalDateTime.of(lastYear, currentMonth, startOfMonthLastYear.toLocalDate().lengthOfMonth(), 23, 59, 59);

        // 格式化日期和时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<String> result = new ArrayList<>(2);
        result.add(startOfMonthLastYear.format(formatter));
        result.add(endOfMonthLastYear.format(formatter));
        return result;
    }

    /**
     * 获取上月的开始、结束日期
     * @return 上月的开始、结束日期
     */
    public static List<String> getLastMonth() {
        LocalDate today = LocalDate.now();
        // 获取上月的开始时间和结束时间
        LocalDate lastMonthStart = today.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastMonthEnd = today.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
        List<String> result = new ArrayList<>(2);
        result.add(lastMonthStart.toString() + " 00:00:00");
        result.add(lastMonthEnd.toString() + " 23:59:59");
        return result;
    }



    /**
     * 根据时间类型获取本月，近8周，近12个月开始、结束日期
     * @return
     */
    public static List<String> getStartTimeAndEndTimeByDateTypeEnum(DateTypeEnum dateTypeEnum){
        List<String> result = new ArrayList<>(2);
        // 获取当前日期
        LocalDate today = LocalDate.now();

        switch (dateTypeEnum){
            case DAY:
                // 获取本月的开始时间（月初）
                LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
                // 获取本月的结束时间（月末）
                LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
                // 如果最后天的结束时间在今天之后，调整为今天
                if (endOfMonth.isAfter(today)) {
                    endOfMonth = today;
                }
                result.add(startOfMonth + " 00:00:00");
                result.add(endOfMonth + " 23:59:59");
                return result;
            case WEEK:
                // 获取第一周的开始时间（8周前的那一周的星期一）
                LocalDate firstWeekStart = today.minusWeeks(7).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

                // 获取最后一周的结束时间（即本周的星期日）
                LocalDate lastWeekEnd = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                // 如果最后一周的结束时间在今天之后，调整为今天
                if (lastWeekEnd.isAfter(today)) {
                    lastWeekEnd = today;
                }
                result.add(firstWeekStart + " 00:00:00");
                result.add(lastWeekEnd + " 23:59:59");
                return result;
            case MONTH:
                // 获取最近12个月的开始日期
                LocalDate firstMonthStart = today.minusMonths(11).withDayOfMonth(1);

                // 获取最新一个月的结束日期
                YearMonth latestMonth = YearMonth.from(today);
                LocalDate latestMonthEnd = latestMonth.atEndOfMonth();

                // 如果最新一个月的结束日期超过今天，则调整为今天
                if (latestMonthEnd.isAfter(today)) {
                    latestMonthEnd = today;
                }
                result.add(firstMonthStart + " 00:00:00");
                result.add(latestMonthEnd + " 23:59:59");
                return result;
            default:
                return null;
        }
    }

    /**
     * 根据时间类型获取当天，当周，当月、的开始时间和结束日期
     * @return
     */
    public static List<String> getCurrentStartTimeAndEndTimeByDateTypeEnum(DateTypeEnum dateTypeEnum){
        List<String> result;
        switch (dateTypeEnum){
            case DAY:
                // 当天
                result = DateUtils.getTodayRange();
                return result;
            case WEEK:
                // 本周
                result = DateUtils.getCurrentWeek();
                return result;
            case MONTH:
                // 本月
                result = DateUtils.getCurrentMonth();
                return result;
            default:
                return null;
        }
    }

    public static String getLastTwelveMonthsFirstDay() {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 计算十二个月前的日期
        LocalDate twelveMonthsAgo = today.minusMonths(12);

        // 获取十二个月前月份的第一天
        LocalDate firstDayOfMonth = twelveMonthsAgo.withDayOfMonth(1);

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 格式化日期
        return firstDayOfMonth.format(formatter);
    }

    public static boolean xAxisEquals(String xAxis, String date, DateTypeEnum dateType) {
        switch (dateType) {
            case DAY:
                return Objects.equals(montageYear(xAxis), date);
            case WEEK:
                return Objects.equals(xAxis, getWeekRange(date));
            case MONTH:
                return Objects.equals(xAxis, date.substring(2,4) + "/" + date.substring(5, 7));
        }
        return false;
    }
    public static String montageYear(String targetDateStr) {
        // 当前日期
        LocalDate today = LocalDate.now();
        // 拼接完整日期
        LocalDate fullDate = LocalDate.of(today.getYear(), today.getMonth(), Integer.parseInt(targetDateStr));
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return fullDate.format(outputFormatter);
    }
    public static String getWeekRange(String inputDateStr) {

        // 定义日期格式化器
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd");

        // 解析输入日期
        LocalDate inputDate = LocalDate.parse(inputDateStr, inputFormatter);

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 计算该日期所在周的开始日期和结束日期
        // 假设周日为一周的开始日
        LocalDate startOfWeek = inputDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate endOfWeek = startOfWeek.plusDays(6); // 周六

        // 确保结束日期不超过当前日期
        if (endOfWeek.isAfter(currentDate)) {
            endOfWeek = currentDate;
        }

        // 格式化并输出结果
        String startFormatted = startOfWeek.format(outputFormatter);
        String endFormatted = endOfWeek.format(outputFormatter);

        return startFormatted + "-" + endFormatted;
    }

    public static List<String> getXAxisByDateTypeEnum(DateTypeEnum dateType) {
        switch (dateType) {
            case DAY:
                return getDaysOfMonth();
            case WEEK:
                return getLastEightWeeks();
            case MONTH:
               return DateUtils.getLastTwelveMonths();
        }
        return null;
    }
    public static List<String> getAllDaysOfMonth() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        LocalDate startOfMonth = today.withDayOfMonth(1);
        LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());

        List<String> dates = new ArrayList<>();
        for (LocalDate date = startOfMonth; !date.isAfter(endOfMonth); date = date.plusDays(1)) {
            dates.add(date.format(formatter));
        }
        return dates;
    }

    public static List<String> getPastTimeByDateTypeEnum(DateTypeEnum dateType) {
        // 获取昨天的日期
        LocalDate day;
        LocalDate pastDay;
        if(dateType == DateTypeEnum.DAY){
            day = LocalDate.now().minusDays(1);
            pastDay = LocalDate.now().minusDays(30);
        } else if(dateType == DateTypeEnum.WEEK){
            LocalDate today = LocalDate.now();
            // 获取本周的开始时间和结束时间
            LocalDate thisWeekStart = today.with(DayOfWeek.MONDAY);
            day = thisWeekStart.minusDays(1);
            pastDay = thisWeekStart.minusDays(30);
        } else {
            LocalDate today = LocalDate.now();
            // 获取本月的开始时间和结束时间
            LocalDate thisMonthStart = today.with(TemporalAdjusters.firstDayOfMonth());
            day = thisMonthStart.minusDays(1);
            pastDay = thisMonthStart.minusDays(30);
        }
        // 定义开始时间和结束时间
        LocalDateTime startOfYesterday = pastDay.atStartOfDay();
        LocalDateTime endOfYesterday = day.atTime(LocalTime.MAX);

        // 输出开始时间和结束时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        List<String> result = new ArrayList<>(2);
        result.add(startOfYesterday.format(formatter));
        result.add(endOfYesterday.format(formatter));
        return result;
    }
    public static List<String> getPastTimeByTime(String time) {
        // 输出开始时间和结束时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate day = LocalDate.parse(time, formatter).minusDays(1);
        LocalDate pastDay = day.minusDays(30);

        // 定义开始时间和结束时间
        LocalDateTime startOfYesterday = pastDay.atStartOfDay();
        LocalDateTime endOfYesterday = day.atTime(LocalTime.MAX);

        List<String> result = new ArrayList<>(2);
        result.add(startOfYesterday.format(formatter));
        result.add(endOfYesterday.format(formatter));
        return result;
    }
    public static boolean isDateInRange(String dateToCheck, String startDateTime, String endDateTime) {
        // 定义日期时间格式
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 解析字符串为LocalDate和LocalDateTime
        LocalDate date = LocalDate.parse(dateToCheck, dateFormatter);
        LocalDateTime startDate = LocalDateTime.parse(startDateTime, dateTimeFormatter);
        LocalDateTime endDate = LocalDateTime.parse(endDateTime, dateTimeFormatter);

        // 将LocalDate转换为LocalDateTime（将时间设置为一天的开始，即00:00:00）
        LocalDateTime dateAsDateTime = date.atStartOfDay();

        // 比较
        return (dateAsDateTime.isEqual(startDate) || dateAsDateTime.isAfter(startDate)) &&
                (dateAsDateTime.isEqual(endDate) || dateAsDateTime.isBefore(endDate));
    }
}
